package menus;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext; 
import javax.faces.model.SelectItem;
import lecture.Lecture;
import lecture.LectureManagedBean;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="editMenuBean")
@CustomScoped(value="#{window}")
public class EditMenuManagedBean {
    
    private List<MenuItemContainer> items;
    
    private List<SelectItem> selectItems;
    
    public EditMenuManagedBean() {
        this.init();
    }

    private void init() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<LectureMenuItem> list = new ArrayList<LectureMenuItem>(lecture.getMenuItems());
        List<MenuItemContainer> beanList = new ArrayList<MenuItemContainer>();
        MenuItemContainer m = null;
        List<SelectItem> selectList = new ArrayList<SelectItem>();
        selectList.add(new SelectItem(null,"Kein Parent"));
        
        for (MenuCategory cat: lecture.getCategorys()) {
            selectList.add(new SelectItem(cat, cat.getName()));
            for (LectureMenuItem l: cat.getMenuItems())   {    
                 
                m = new MenuItemContainer(l,cat);
                beanList.add(m);
                list.remove(m.getItem());
            }
        }
        
        for (LectureMenuItem l: list) {
            beanList.add(new MenuItemContainer(l));
        }
        
        this.items = beanList;
        this.selectItems = selectList;
    }

    public List<MenuItemContainer> getItems() {
        this.init();
        return items;
    }

    public List<SelectItem> getSelectItems() {
        this.init();
        return selectItems;
    }
    
    public List<CategoryContainer> getCategorys() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        List<CategoryContainer> list = new ArrayList<CategoryContainer>();
        for (MenuCategory cat: lecture.getCategorys()) {
            list.add(new CategoryContainer((cat)));
        }
        return list;
    }
}
